package com.atguigu.tingshu.payment.strategy;

import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.payment.config.WxPayV3Config;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.payment.WeChatPayVo;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.payments.jsapi.model.Amount;
import com.wechat.pay.java.service.payments.jsapi.model.Payer;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse;

import java.util.Date;

/**
 * Author  王东剑
 * Created on 25/6/2024 PM5:30
 * Package_name com.atguigu.tingshu.payment.strategy
 * Description 支付策略类,区分支付和充值
 */
public interface PaymentStrategy {
    public abstract WeChatPayVo createJsapi(String paymentType, String orderNo);
    public abstract String getPaymentType();

    default public WeChatPayVo setParamsAndGetWeChatPayVo(WxPayV3Config wxPayV3Config, UserInfoFeignClient userInfoFeignClient,String description,String orderNo){
        // 获取微信请求前的配置
        Config config = wxPayV3Config.getConfig();
        // 根据 userId 获取用户信息
        Long userId = AuthContextHolder.getUserId();
        UserInfo userInfo = userInfoFeignClient.findUserById(userId).getData();
        // 构造发送对象
        JsapiServiceExtension service = new JsapiServiceExtension.Builder().config(config).build();
        // 跟之前下单示例一样，填充预下单参数
        PrepayRequest prepayRequest = new PrepayRequest();
        prepayRequest.setAppid(wxPayV3Config.getAppid());
        prepayRequest.setMchid(wxPayV3Config.merchantId);
        prepayRequest.setDescription(description);
        prepayRequest.setOutTradeNo(orderNo);
        prepayRequest.setNotifyUrl(wxPayV3Config.getNotifyUrl());
        Amount amount = new Amount();
        // TODO 假的支付金额,方便后期测试,这个单位是分
        amount.setTotal(1);
        prepayRequest.setAmount(amount);
        // 支付者信息
        Payer payer = new Payer();
        payer.setOpenid(userInfo.getWxOpenId());
        prepayRequest.setPayer(payer);

        // response包含了调起支付所需的所有参数，可直接用于前端调起支付
        // TODO: 正常的调用已经给注释了,因为没有appid和商户id等等东西
        // PrepayWithRequestPaymentResponse response = service.prepayWithRequestPayment(prepayRequest);
        PrepayWithRequestPaymentResponse response = new PrepayWithRequestPaymentResponse();
        response.setAppId(wxPayV3Config.getAppid());
        response.setTimeStamp(String.valueOf(new Date().getTime()));
        response.setNonceStr("setNonceStr");
        response.setPackageVal("setPackageVal");
        response.setSignType("setSignType");
        response.setPaySign("setPaySign");

        // 获取结果数据封装到 WeChatPayVo
        WeChatPayVo weChatPayVo = new WeChatPayVo();
        weChatPayVo.setTimeStamp(response.getTimeStamp());
        weChatPayVo.setNonceStr(response.getNonceStr());
        weChatPayVo.setPackageVal(response.getPackageVal());
        weChatPayVo.setSignType(response.getSignType());
        weChatPayVo.setPaySign(response.getPaySign());
        return weChatPayVo;
    }
}
